package com.example.beat_x3.Fragments;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.beat_x3.R;

import Entity.Inf;

public class CenterFragment extends MyFragment {
    public TextView student_account, music_preference, student_name, credit, borrow_count;
    public ImageView sex;
    public MyReservationFragment myreservationFragment;

    CenterFragment() {
        myreservationFragment = new MyReservationFragment();
    }

    @SuppressLint("SetTextI18n")
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        @SuppressLint("InflateParams") View view = inflater.inflate(R.layout.fragment_usercenter, null);
        view.findViewById(R.id.center_button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Inf.isLog = false;
                Inf.stuInf = null;
                Inf.contactFragment = null;
                Inf.rentalFragment = null;
                Inf.chatFragment = null;
                Inf.feedbackFragment = null;
                Inf.myinstrumentFragment=null;
                Inf.contacts.clear();
                changeFragment(Inf.userFragment);
            }
        });
        view.findViewById(R.id.wodeyuyue).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myreservationFragment.refresh();
                changeFragment(myreservationFragment);
            }
        });
        view.findViewById(R.id.center_changebutton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Inf.changingFragment == null) {
                    Inf.changingFragment = new ChangingFragment();
                }
                changeFragment(Inf.changingFragment);
            }
        });
        view.findViewById(R.id.user_message).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Inf.contactFragment == null) {
                    Inf.contactFragment = new ContactFragment();
                }
                changeFragment(Inf.contactFragment);
            }
        });
        view.findViewById(R.id.center_rental).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Inf.rentalFragment == null) {
                    Inf.rentalFragment = new RentalFragment();
                }
                changeFragment(Inf.rentalFragment);
            }
        });
        view.findViewById(R.id.center_complaint).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Inf.complaintFragment == null) {
                    Inf.complaintFragment = new ComplaintFragment();
                }
                changeFragment(Inf.complaintFragment);
            }
        });
        view.findViewById(R.id.center_feedback).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Inf.feedbackFragment == null) {
                    Inf.feedbackFragment = new FeedbackFragment();
                }
                changeFragment(Inf.feedbackFragment);
            }
        });
        student_account = view.findViewById(R.id.center_name);
        student_name = view.findViewById(R.id.student_name);
        music_preference = view.findViewById(R.id.music_preference);
        credit = view.findViewById(R.id.credit);
        borrow_count = view.findViewById(R.id.borrow_count);
        sex = view.findViewById(R.id.sex);
        student_account.setText(Inf.stuInf.StudentAccount);
        student_name.setText(Inf.stuInf.StudentName);
        music_preference.setText("音乐偏好:" + Inf.stuInf.MusicPreference);
        borrow_count.setText("借阅次数:" + Inf.stuInf.BorrowCount);
        sex.setImageResource(Inf.stuInf.StudentSex ? R.drawable.baseline_male_24 : R.drawable.baseline_female_24);
        if (Inf.stuInf.BorrowCount > 0) {
            credit.setText("信誉度:" + String.format("%.2f", Inf.credit * 100) + "%");
            credit.setTextColor(Color.rgb(255 - Inf.COLOR, Inf.COLOR, 0));
        } else {
            credit.setText("信誉度:~");
        }
        return view;
    }
}
